﻿using GameBasic;
using System.Collections.Generic;
using UnityEngine;

namespace VCity
{
    public class ClientMain : MonoSingleton<ClientMain>
    {
        public enum Scenario
        {
            None,
            Login = 1,
            Char = 8
        }

        [Header("Init")]
        public bool toggleFirstPersonCam;
        public bool allowOffline = true;
        public Scenario goScenario;

        [Header("Ref")]
        public CharCtrl charCtrl;
        // runtime

        private void Start()
        {
#if !UNITY_EDITOR
            goScenario = Scenario.Login;
#endif
            // update vcam
            switch (goScenario)
            {
                case Scenario.None:
                    break;
                case Scenario.Login:
                    GoLogin();
                    break;
                case Scenario.Char:
                    GoGameplay(0);
                    break;
                default:
                    break;
            }
            //Debug.Log("ClientMainStart");

            // disable charCtrl
            charCtrl.gameObject.SetActive(!UIMain.Instance.loginUI.gameObject.activeSelf);
        }

        private void Update()
        {
            // toggle first person cam
            var vcams = VCams.Instance;
            if (vcams != null && toggleFirstPersonCam)
            {
                toggleFirstPersonCam = false;
                vcams.SetFirstPersonCam(!vcams.isFirstPersonCam);
            }
        }

        public void GoLogin()
        {
            UIMain.Instance.loginUI.gameObject.SetActive(true);
            VCams.Instance.characterCam.gameObject.SetActive(false);
        }

        public void GoGameplay(int charMeshIndex)
        {
            if (charCtrl != null)
            {
                // set skin
                var meshes = GameResources.Instance.characterMeshes;
                if (meshes.IsLegalIndex(charMeshIndex))
                    charCtrl.meshObj.sharedMesh = meshes[charMeshIndex];

                VCams.Instance.characterCam.gameObject.SetActive(true);

                // ui
                UIMain.Instance.loginUI.gameObject.SetActive(false);

                // enable charCtrl
                charCtrl.gameObject.SetActive(true);
            }
        }

        public bool PlayOffline(int meshIndex)
        {
            if (allowOffline)
                GoGameplay(meshIndex);
            return allowOffline;
        }
    }
}